﻿using ITXHWEB.Models;
using ReflectionStudio.Core.Events;
using System;
using System.Data;
using System.Web.Mvc;
using WPFExcelReport;
using WPFExcelReport.Helpers;
using ITXHWEB.Infrastructure;
using System.Collections;
using System.Collections.Generic;
using System.Linq;

namespace ITXHWEB.Controllers
{
    public partial class WBBSTController : ITXController
    {
        [ChildActionOnly]
        public ActionResult MPBWZ04(string returnUrl)
        {
            ModelState.Clear();
            ViewBag.IsPDCReady = MWB.IsPDCReady;
            ViewBag.IsABPReady = MWB.IsABPReady;
            ViewBag.IsTPReady = MWB.IsTPReady;
            ViewBag.IsGEBNRReady = MWB.IsGEBNRReady;  
            ViewBag.ReturnUrl = returnUrl;
            MWB.REMARK = INFO;
            MWB.INFO = "";
            foreach (KeyValuePair<string,string> kp in MWB.WHPCS)
            {
                string[] vu = kp.Value.Split('{');                
                MWB.INFO +=kp.Key + "批," + vu[0] + "卷," + vu[1].Replace("}","") + "米"+Environment.NewLine;
            }
            return PartialView(MWB);
        }
        [HttpPost]
        [AllowAnonymous]
        [ValidateAntiForgeryToken]
        public ActionResult MPBWZ04(WBBSTModel model, string returnUrl)
        {

            try
            {
                StartNewJob();
                //WBW.POSTCHG();
            }
            catch (Exception ex)
            {
                //VKPAUF = ex.Message;
                Tracer.Error(ex.StackTrace, ex);
            }
            return RedirectToAction(MAPP, MAPP);
        }
        public void StartNewJob()
        {
            MWB = new WBBSTModel(MWB);
            MWB.NEWUSER = CUSER;
            MWB.WHPCS.Clear();
            MWB.INFO = "";
            INFO = "请扫描布卷";
        }
         
        [ChildActionOnly]
        public ActionResult PBWZ04(string returnUrl)
        {
            ModelState.Clear();
            ViewBag.ReturnUrl = returnUrl; 
            return PartialView(MWB);
        }
        [HttpPost]
        [AllowAnonymous]
        [ValidateAntiForgeryToken]
        public ActionResult PBWZ04(WBBSTModel model, string returnUrl)
        {
            try
            {
                PB2WZ(model);//移库操做  
            }
            catch (Exception ex)
            {
                INFO = ex.Message;
                //ModelState.AddModelError("",ex.Message);
                Tracer.Error(ex.StackTrace, ex);
            }
            return RedirectToAction(MAPP, MAPP);
        } 
        public void PB2WZ(WBBSTModel model)
        {
            if (model == null)
                return; 
            if (model.WBBSTKNR == null)
                return; 
            model.WBBSTKNR = model.WBBSTKNR.Trim().PadRight(12);
            dwData = WBB.TBMain.NewRow();
            dwData["WBBMANDANT"] = app.Mandant;
            dwData["WBBAUFTYP"] = "V";
            dwData["WBBAUFKRS"] = MWB.ABPAUF.Substring(0, 2);
            dwData["WBBAUFLFD"] = MWB.ABPAUF.Substring(2, 6);
            dwData["WBBAUFPOS"] = MWB.ABPAUF.Substring(8, 3);
            dwData["WBBSTKNR"] = model.WBBSTKNR.Trim().PadRight(12); 
            DataSet SReult = WBB.QWB24(dwData); 
            if (SReult.Tables[0].Rows.Count == 0)
            {
                MWB.IsDataValid = false;
                INFO = "不在范围内";
                return;
            }
            MWB.IsDataValid = true;
            DataRow _dwWB = SReult.Tables[0].Copy().Rows[0];
            Helper.GetSTime(ref date, ref time, ref stime);
            _dwWB["WBBAUFTYP"] = "V";
            _dwWB["WBBAUFKRS"] = MWB.ABPAUF.Substring(0,2);
            _dwWB["WBBAUFLFD"] = MWB.ABPAUF.Substring(2, 6);
            _dwWB["WBBAUFPOS"] = MWB.ABPAUF.Substring(8, 3);
            _dwWB["WBBTP"] = MWB.NEWTP;
            _dwWB["WBBGEBNR"] = MWB.GEBNR;
            _dwWB["WBBAENDBEN"] = CUSER;
            _dwWB["WBBAENDDAT"] = date;
            PM["WBROW"] = _dwWB;
            PM["RUNID"] = RUNID;
            WBB.WBB61(PM);           
            INFO = "完成(" + model.WBBSTKNR.Trim() + ")";
            double mtr= Convert.ToDouble(_dwWB["WBBISTKLG"].ToString()) * 0.001;
            if (MWB.WHPCS.ContainsKey(MWB.NEWTP))
            {
                string[] vu = MWB.WHPCS[MWB.NEWTP].Split('{');
                int pcs = Convert.ToInt32(vu[0]);
                double lg = Convert.ToInt32(vu[1].Replace("}", ""));
                pcs++;
                lg += mtr;
                MWB.WHPCS[MWB.NEWTP] =   (pcs).ToString() + "{" + (lg).ToString() + "}";
            }
            else
            {
                MWB.WHPCS.Add(MWB.NEWTP, "1{" + mtr + "}");

            }
            
        } 
    }
}
